<template>
  <div class="user-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>用户列表</span>
        <el-button
            style="float: right; padding: 3px 0"
            type="text"
            @click="handleAdd"
        >添加用户</el-button>
      </div>

      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="username" label="用户名" width="150"></el-table-column>
        <el-table-column prop="realName" label="真实姓名" width="120"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
        <el-table-column prop="phone" label="电话" width="150"></el-table-column>
        <el-table-column prop="role" label="角色" width="100">
          <template slot-scope="scope">
            {{ formatRole(scope.row.role) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getUserList, deleteUser } from '@/api/user'

export default {
  name: 'UserList',
  data() {
    return {
      userList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      getUserList(params).then(response => {
        this.userList = response.data.list
        this.total = response.data.total
      })
    },
    formatRole(role) {
      const roles = {
        1: '管理员',
        2: '教师',
        3: '学生'
      }
      return roles[role] || '未知'
    },
    handleAdd() {
      this.$router.push('/user/add')
    },
    handleEdit(user) {
      this.$router.push(`/user/edit/${user.id}`)
    },
    handleDelete(id) {
      this.$confirm('确认删除该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then(() => {
          this.$message.success('删除成功')
          this.getUserList()
        })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getUserList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getUserList()
    }
  }
}
</script>

<style lang="scss" scoped>
.user-container {
  padding: 20px;
}
</style>